home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / gfx / show / gs503_data.lha / Ghostscript / data / gslp.ps < prev    next >
Text File  |  1997-08-14  |  16KB  |  533 lines

  1. %    Copyright (C) 1991, 1995, 1996, 1997 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of Aladdin Ghostscript.
  3. % Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  4. % or distributor accepts any responsibility for the consequences of using it,
  5. % or for whether it serves any particular purpose or works at all, unless he
  6. % or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  7. % License (the "License") for full details.
  8. % Every copy of Aladdin Ghostscript must include a copy of the License,
  9. % normally in a plain ASCII text file named PUBLIC.  The License grants you
  10. % the right to copy, modify and redistribute Aladdin Ghostscript, but only
  11. % under certain conditions described in the License.  Among other things, the
  12. % License requires that the copyright notice and this notice be preserved on
  13. % all copies.
  14.  
  15. % gslp.ps - format and print text
  16.  
  17. % This utility provides functionality approximately equivalent to the Unix
  18. % `enscript' program.  It prints plain text files using a single font.
  19. % It currently handles tabs and formfeeds, but not backspaces.
  20. % It only works with fixed-pitch fonts.
  21. % Standard flags implemented:
  22. %    -12BclqRr -b<header> -f<font> -F<hfont> -L<lines> -p<outfile>
  23. % Sun flags implemented:
  24. %    -T<n>    set tab width
  25. % Flags ignored:
  26. %    -GghKkmow -# -C -d -J -n -P -S -s -t -v
  27. % Flags added:
  28. %    --columns <n>
  29. %        print in <n> columns
  30. %    --detect
  31. %        treat the file as PostScript if it starts with %!
  32. %    --first-page <n>
  33. %        start printing at page <n>
  34. %    --last-page <n>
  35. %        stop printing after page <n>
  36. %    --(heading|footing)-(left|center|right) <string>
  37. %        set the heading/footing fields; use -B first to clear
  38. %    --margin-(top|bottom|left|right) <inches>
  39. %        set a margin
  40. %    --no-eject-(file|formfeed)
  41. %        end-of-file/FF only starts a new column, not a new sheet
  42. %    --spacing <n>
  43. %        use double (n=2), triple (n=3), etc. spacing
  44. % Also, the string %# in a heading or footing is replaced with the page #.
  45. /PageNumberString (%#) def
  46.  
  47. /lpdict 150 dict def
  48. lpdict begin
  49.  
  50. % build iso-latin-1 version of a font
  51. /font-to-iso-latin-1 {    % <font> font-to-iso-latin-1 <font>
  52.     %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  53.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  54.     /Encoding ISOLatin1Encoding def currentdict end
  55.     dup /FontName get 80 string cvs (-ISOLatin1) concatstrings cvn 
  56.     exch definefont
  57. } def
  58.  
  59. /find-latin-font {    % <name> find-latin-font <font>
  60.   findfont font-to-iso-latin-1
  61. } def
  62.  
  63. % Define the initial values of the printing parameters.
  64.  
  65. /BodyFont null def        % use default
  66.   /defaultBodyFont
  67.     { /Courier find-latin-font Landscape { 7 } { 10 } ifelse scalefont } def
  68. /Columns 1 def
  69. /DetectFileType false def
  70. /EjectEOF true def
  71. /EjectFF true def
  72. /Footers false def
  73. /FootingLeft () def
  74. /FootingCenter () def
  75. /FootingRight () def
  76. /Headers true def
  77. /HeadingLeft () def
  78. /HeadingCenter () def
  79. /HeadingRight (page ) PageNumberString concatstrings def
  80. /HeadingFont null def        % use default
  81.   /defaultHeadingFont
  82.     { /Courier-Bold find-latin-font 10 scalefont } def
  83. /Landscape false def
  84. /MarginBottom 36 def        % 1/2"
  85. /MarginLeft 36 def        % 1/2"
  86. /MarginRight 36 def        % 1/2"
  87. /MarginTop 36 def        % 1/2"
  88. /MaxLines 9999 def        % max lines per page
  89. /Noisy true def            % i.e., not quiet
  90. /OutFile null def        % null = write directly to device
  91. /PageFirst 1 def
  92. /PageLast 99999 def
  93. /Spacing 1 def
  94. /Tab 8 def
  95. /Truncate false def        % wrap long lines, don't truncate
  96.  
  97. % When writing to a file, we want to write out PostScript;
  98. % when writing to the printer, we want to execute it;
  99. % some commands should be executed regardless.
  100. % lpexec provides for all this.
  101.  
  102. /lpexec        % <arg1> ... <argn> </op> <n> <do_always> lpexec -
  103.  { OutFile null eq
  104.     { pop 1 add true
  105.     }
  106.     { /t exch def 1 add /n exch def cvx
  107.       n { n -1 roll dup wosp } repeat
  108.       OutFile (\n) writestring
  109.       n t
  110.     }
  111.    ifelse
  112.     { pop load exec }
  113.     { { pop } repeat }
  114.    ifelse
  115.  } def
  116.  
  117. /lpmoveto
  118.  { /moveto 2 true lpexec
  119.  } def
  120. /lpshow
  121.  { dup length 0 ne { /show 1 true lpexec } { pop } ifelse
  122.  } def
  123. /lpsetmyfont
  124.  { dup load setfont
  125.    OutFile null ne { cvx /setfont 1 false lpexec } { pop } ifelse
  126.  } def
  127.  
  128. % Define some utility procedures.
  129.  
  130. /banner        % ypos left center right
  131.  { /HFont lpsetmyfont
  132.    3 -1 roll bannerstring pop 0 4 index pwidth showline2 pop
  133.    exch bannerstring pwidth exch sub 2 div 3 index pwidth showline2 pop
  134.    bannerstring pwidth exch sub
  135.    3 -1 roll pwidth showline2 pop
  136.  } def
  137.  
  138. /bannerstring    % string -> string width
  139.   { PageNumberString search
  140.      { exch pop pindex 4 string cvs concatstrings exch concatstrings
  141.      }
  142.     if dup stringwidth pop
  143.   } def
  144.  
  145. /beginpage
  146.  { /lindex 0 def
  147.    /skipping pindex PageFirst ge pindex PageLast le and not def
  148.    /save 0 true lpexec /pagesave exch def
  149.    skipping { nulldevice   /OutFile null def } if
  150.    Headers
  151.     { lheight hdescent add
  152.       HeadingLeft HeadingCenter HeadingRight banner
  153.     } if
  154.    /BFont lpsetmyfont
  155.  } def
  156.  
  157. /endpage
  158.  { lindex 0 ne
  159.     { Footers
  160.        { topskip plength sub hdescent add
  161.          FootingLeft FootingCenter FootingRight banner
  162.        } if
  163.       /showpage 0 false lpexec
  164.     } if
  165.    pagesave /restore 0 true lpexec
  166.    /pindex pindex 1 add def
  167.  } def
  168.  
  169. /endcolumn
  170.  { lindex colines 1 sub add colines idiv colines mul
  171.    dup llength ge { pop endpage beginpage } { /lindex exch def } ifelse
  172.  } def
  173.  
  174. /fontheight    % <font> fontheight <ascent> <height>
  175.  { gsave setfont
  176.    newpath 0 0 moveto
  177.    (|^_j) false charpath
  178.    pathbbox exch pop dup 2 index sub 4 -2 roll pop pop
  179.    grestore exch 1.25 mul exch 1.25 mul
  180.  } def
  181.  
  182. /wosp
  183.  { OutFile ( ) writestring OutFile exch write==only
  184.  } def
  185.  
  186. /outfont        % name font ->
  187.  { OutFile null ne
  188.     { exch wosp
  189.       dup /FontName get 
  190.       dup wosp OutFile ( findfont) writestring
  191.       %% reencode for iso latin1; from the 2nd edition red book, sec 5.6.1
  192.       OutFile ( 
  193.     dup length dict begin {1 index /FID ne {def} {pop pop} ifelse} forall
  194.     /Encoding ISOLatin1Encoding def currentdict end
  195.       ) writestring
  196.       wosp OutFile (-ISOLatin1 exch definefont) writestring
  197.       /FontMatrix get 0 get 1000 mul round cvi wosp
  198.       OutFile ( scalefont def\n) writestring
  199.     }
  200.     { pop pop
  201.     }
  202.    ifelse
  203.  } def
  204.  
  205. /StringFF (\f) def
  206. /CharFF StringFF 0 get def
  207. /StringTAB (\t) def
  208. /CharTAB StringTAB 0 get def
  209.  
  210. /showline        % line -> leftover_line (handles \f)
  211.  {  { showline1 dup length 0 eq { exit } if
  212.       dup 0 get CharFF ne { exit } if
  213.       EjectFF { endpage beginpage } { endcolumn } ifelse
  214.       skip1
  215.     }
  216.    loop
  217.  } def
  218.  
  219. /showline1        % line -> leftover_line (handles page break)
  220.  { lindex llength eq { endpage beginpage } if
  221.    lindex colines idiv cowidth mul        % x
  222.    lindex colines mod 1 add lheight mul neg fascent sub    % y
  223.    1 index cowidth add
  224.    showline2
  225.    /lindex lindex 1 add def
  226.  } def
  227.  
  228. /showline2    % string x y xlimit -> leftover_string (handles tabs)
  229.  { 2 index exch 5 2 roll lpmoveto    % xinit xlimit string
  230.     { showline3 dup length 0 eq { exit } if
  231.       dup 0 get CharTAB ne { exit } if
  232.       currentpoint exch 4 index sub tabwx div
  233.     0.05 add ceiling tabwx mul 4 index add exch lpmoveto
  234.       skip1
  235.       currentpoint pop 2 index ge { exit } if
  236.     }
  237.    loop exch pop exch pop
  238.  } def
  239.  
  240. /showline3    % xlimit string -> xlimit leftover_string
  241.         % (finds line break / tab / formfeed)
  242.  { currentpoint pop 2 index exch sub
  243.      cwx div 0.1 add cvi 0 max 1 index length min
  244.    1 index 0 3 -1 roll getinterval
  245.     % look for \f or \t
  246.    StringFF search { exch pop exch pop } if
  247.    StringTAB search { exch pop exch pop } if
  248.    dup lpshow
  249.    length dup 2 index length exch sub getinterval
  250.  } def
  251.  
  252. /skip1
  253.  { dup length 1 sub 1 exch getinterval
  254.  } def
  255.  
  256. /e== {        % <object> e== - -- print an object to stderr
  257.   (%stderr) (w) file dup 3 -1 roll write==only flushfile
  258. } def
  259.  
  260. /eprint {    % <string> eprint - -- print a string to stderr
  261.   (%stderr) (w) file dup 3 -1 roll writestring flushfile
  262. } def
  263.  
  264. % The main printing procedure
  265.  
  266. /lp        % file initial_chars ->
  267.  { /lpline exch def
  268.    /lpfile exch def
  269.    /save 0 true lpexec
  270.  
  271. % Initialize the device and fonts.
  272.    /BFont
  273.      BodyFont null eq { defaultBodyFont } { BodyFont } ifelse def
  274.    /BFont BFont outfont
  275.    Headers Footers or
  276.     { /HFont
  277.     HeadingFont null eq { defaultHeadingFont } { HeadingFont } ifelse def
  278.       /HFont HFont outfont
  279.     }
  280.    if
  281.  
  282. % Get the layout parameters.
  283.    clippath
  284.    Landscape { 90 /rotate 1 true lpexec } if
  285.    BFont setfont ( ) stringwidth pop /cwx exch def
  286.      cwx Tab mul /tabwx exch def
  287.    BFont fontheight /fheight exch def /fascent exch def
  288.    Headers Footers or { HFont fontheight } { 0 0 } ifelse
  289.      /hheight exch def /hascent exch def
  290.      /hdescent hheight hascent sub def
  291.    fheight Spacing mul /lheight exch def
  292.    Headers { hheight lheight add } { 0 } ifelse
  293.      /topskip exch def
  294.    Footers { hheight lheight add } { 0 } ifelse
  295.      /botskip exch def
  296.    /pskip topskip botskip add def
  297.     % Translate the page so that (0,0) corresponds to
  298.     % the top of the topmost body line.
  299.    pathbbox
  300.      2 index sub MarginBottom MarginTop add sub /plength exch def
  301.      2 index sub MarginLeft MarginRight add sub /pwidth exch def
  302.      pwidth Columns div /cowidth exch def
  303.      exch MarginLeft add
  304.      exch MarginBottom add plength add topskip sub
  305.      /translate 2 true lpexec
  306.    plength pskip sub lheight div cvi MaxLines min
  307.      dup /colines exch def
  308.      Columns mul /llength exch def
  309.    OutFile null ne { nulldevice } if
  310.  
  311. % Print layout
  312.    Noisy
  313.     { (Page height = ) eprint llength e==
  314.       (.\n) eprint flush
  315.     } if
  316.  
  317. % Disable stack recording so we can use stopped with readline.
  318.    $error /recordstacks false put
  319.  
  320. % Initialize for the first page.
  321.    /lbuf 1000 string def
  322.    /pindex 1 def
  323.    beginpage
  324.  
  325. % Iterate through the file.
  326.    lpline
  327.     { dup length /pos exch def
  328.       lbuf exch 0 exch putinterval
  329.        { lpfile lbuf pos lbuf length pos sub getinterval readline } stopped
  330.        {    % Filled the line before a CR or EOF.
  331.          exch pop showline
  332.        }
  333.        {    % Reached CR and/or EOF first.
  334.          exch length pos add lbuf exch 0 exch getinterval
  335.      1 index { showline } if        % omit final empty line
  336.       { dup length 0 eq Truncate or { pop () exit } if
  337.         showline
  338.       }
  339.      loop
  340.      exch not { exit } if
  341.        }
  342.       ifelse
  343.     } loop
  344.    pop
  345.  
  346. % Wrap up.
  347. %**************** WHY IS THIS COMMENTED OUT? ****************
  348. %   EjectEOF { endpage } { endcolumn } ifelse
  349.    endpage
  350.    /restore 0 true lpexec
  351.  
  352.  } def
  353.  
  354. end
  355.  
  356. % Usage: <file> lp
  357. %   prints <file> using the current parameter settings.
  358. % Usage: [ <arg1> ... <argn> ] lpcommand
  359. %   interprets args like a command line.
  360.  
  361. /lp { save   lpdict begin () lp end   restore } def
  362.  
  363. lpdict begin
  364.  
  365. /splitfn        % (FontNNN) -> <font>
  366.  { dup /arg exch def length
  367.     { dup 0 le { exit } if
  368.       dup 1 sub arg exch get dup 48 ge exch 59 le and not { exit } if
  369.       1 sub
  370.     } loop
  371.    arg exch 0 exch getinterval dup cvn find-latin-font
  372.    exch arg exch anchorsearch pop pop cvr scalefont
  373.  } def
  374.  
  375. % Parse the command line switches.
  376.  
  377. /doswitch    % argn ... arg1 (-?) restofswitch ->
  378.  { exch dup cvn lpdict exch known
  379.     { cvn load exec }
  380.     { exch pop (Unknown switch: ) eprint eprint (\n) eprint }
  381.    ifelse
  382.  } def
  383.  
  384. /more        % argn ... arg1 restofswitch ->
  385.  { dup length 0 ne
  386.     { (- ) dup 1 3 index 0 get put
  387.       exch dup length 1 sub 1 exch getinterval
  388.       doswitch
  389.     }
  390.     { pop
  391.     }
  392.    ifelse
  393.  } def
  394.  
  395. /-- { (--) exch concatstrings
  396.       dup cvn lpdict exch known
  397.        { cvn load exec }
  398.        { (Unknown switch: ) eprint eprint (\n) eprint }
  399.       ifelse
  400.     } def
  401. /--columns { cvi 1 max /Columns exch def } def
  402. /--detect { /DetectFileType true def } def
  403. /--first-page { cvi /PageFirst exch def } def
  404. /--footing-center { /FootingCenter exch def   /Footers true def } def
  405. /--footing-left { /FootingLeft exch def   /Footers true def } def
  406. /--footing-right { /FootingRight exch def   /Footers true def} def
  407. /--heading-center { /HeadingCenter exch def   /Headers true def } def
  408. /--heading-left { /HeadingLeft exch def   /Headers true def } def
  409. /--heading-right { /HeadingRight exch def   /Headers true def } def
  410. /--last-page { cvi /PageLast exch def } def
  411. /--margin-bottom { cvr 72.0 mul /MarginBottom exch def } def
  412. /--margin-left { cvr 72.0 mul /MarginLeft exch def } def
  413. /--margin-right { cvr 72.0 mul /MarginRight exch def } def
  414. /--margin-top { cvr 72.0 mul /MarginTop exch def } def
  415. /--no-eject-file { /EjectEOF false def } def
  416. /--no-eject-formfeed { /EjectFF false def } def
  417. /--spacing { cvr /Spacing exch def } def
  418.  
  419. /-# { pop } def        % ignore
  420. /-+ { -- } def
  421. (-1)cvn { /Columns 1 def   more } def
  422. (-2)cvn { /Columns 2 def   more } def
  423. /-b { /HeadingLeft exch def   /HeadingCenter () def   /HeadingRight PageNumberString def
  424.       /Headers true def
  425.       /break true def
  426.     } def
  427. /-B { /HeadingLeft () def   /HeadingCenter () def   /HeadingRight () def
  428.       /Headers false def
  429.       /FootingLeft () def   /FootingCenter () def   /FootingRight () def
  430.       /Footers false def
  431.       /break true def
  432.       more
  433.     } def
  434. /-C { pop } def        % ignore
  435. /-c { /Truncate true def   more } def
  436. /-d { pop } def        % ignore
  437. /-f { splitfn /BodyFont exch def } def
  438. /-F { splitfn /HeadingFont exch def } def
  439. /-G { more } def    % ignore
  440. /-g { more } def    % ignore
  441. /-h { more } def    % ignore
  442. /-J { pop } def        % ignore
  443. /-K { more } def    % ignore
  444. /-k { more } def    % ignore
  445. /-l { 66 -L -B } def
  446. /-L { cvi /MaxLines exch def } def
  447. /-m { more } def    % ignore
  448. /-n { pop } def        % ignore
  449. /-o { more } def    % ignore
  450. /-p { (w) file /OutFile exch def   OutFile (%!\n) writestring } def
  451. /-P { pop } def        % ignore
  452. /-q { /Noisy false def   more } def
  453. /-r { /Landscape true def   more } def
  454. /-R { /Landscape false def   more } def
  455. /-S { pop } def        % ignore
  456. /-s { pop } def        % ignore
  457. /-T { cvi /Tab exch def } def
  458. /-v { pop } def        % ignore
  459. /-w { more } def    % ignore
  460.  
  461. /lp1        % filename ->
  462.  { break not { dup /HeadingLeft exch def } if
  463.    Noisy
  464.     { (Printing ) eprint dup eprint (\n) eprint 
  465.     } if
  466.    (r) file
  467.         % If requested, check for a PostScript file.
  468.    DetectFileType
  469.     { dup 2 string readstring pop dup (%!) eq
  470.        {    % Yes, it's a PostScript file.
  471.          pop dup 80 string readline pop pop cvx exec
  472.        }
  473.        { lp
  474.        }
  475.       ifelse
  476.     }
  477.     { () lp
  478.     }
  479.    ifelse
  480.  } bind def
  481.  
  482. /lpcstring 100 string def
  483.  
  484. end
  485.  
  486. /lpcommand        % <[arg1 ... argn]> lpcommand <any_printed>
  487.  {    % Push the commands on the stack in reverse order
  488.    mark exch
  489.    dup length 1 sub -1 0 { 1 index exch get exch } for pop
  490.    lpdict begin
  491.    /any false def
  492.    /break false def
  493.     { dup mark eq { pop exit } if
  494.       dup length 2 ge { dup 0 get (-) 0 get eq } { false } ifelse
  495.        { dup 0 2 getinterval
  496.          exch dup length 2 sub 2 exch getinterval
  497.      doswitch
  498.        }
  499.        { dup  /matched false def
  500.           { /matched true def   /any true def   lp1 } lpcstring filenameforall
  501.      matched { pop } { lp1 } ifelse        % let the error happen
  502.        }
  503.       ifelse
  504.     } loop
  505.    OutFile null ne
  506.     { OutFile (%stdout) (w) file ne { OutFile closefile } if
  507.       /OutFile null def
  508.     } if
  509.    any
  510.    end
  511.  } def
  512.  
  513. [ shellarguments
  514.  { ] dup length 0 ne { lpcommand } { false } ifelse not
  515.     { (%stderr) (w) file
  516.       [ (Usage: )
  517.     /PROGNAME where { pop PROGNAME } { (gslp) } ifelse
  518.     ( [-12BclqRr] [-b<header>] [-f<font>] [-F<hfont>]\n)
  519.     (        [-L<lines>] [-p<outfile>] [-T<tabwidth>] [--columns <n>]\n)
  520.     (        [--detect] [--first-page <page#>] [--last-page <page#>]\n)
  521.     (        [--(heading|footing)-(left|right|center) <string>]\n)
  522.     (        [--margin-(top|bottom|left|right) <inches>]\n)
  523.     (        [--no-eject-(file|formfeed)] [--spacing <n>] file1 ... filen\n)
  524.       ] { 2 copy writestring pop } forall dup flushfile closefile
  525.     }
  526.    if
  527.  }
  528.  { pop }
  529. ifelse
  530.